package com.bw.module_home.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.bw.lib_common.livedata_eventbus.Events
import com.bw.module_home.R
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author ytx
 * @date 2025-10-18 09:51
 * @description
 */
class RedPacketWidget:FrameLayout {
    private lateinit var redPacketView: RedPacketView
    private lateinit var tvNum:TextView

    private lateinit var translateAnimator:ObjectAnimator
    private lateinit var alphaAnimator: ObjectAnimator
    private lateinit var animatorSet: AnimatorSet
    constructor(context: Context) : this(context,null)
    constructor(context: Context, attrs: android.util.AttributeSet?) : this(context, attrs,0)
    constructor(context: Context, attrs: android.util.AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
      initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_redpacket_widget,this,false)
        redPacketView = view.findViewById(R.id.red_packet_view)
        tvNum = view.findViewById(R.id.tv_redpacket_num)
        addView(view)
        tvNum.visibility = View.INVISIBLE

        //初始化tvNum动画
        translateAnimator = ObjectAnimator.ofFloat(tvNum,"translationY",0f,-20f)
        alphaAnimator = ObjectAnimator.ofFloat(tvNum,"alpha",0f,1f,0f)
        //组合动画
        animatorSet = AnimatorSet()
        animatorSet.playTogether(translateAnimator,alphaAnimator)
        animatorSet.duration = 2000
        animatorSet.addListener(object : AnimatorListenerAdapter(){
            override fun onAnimationStart(animation: Animator) {
                super.onAnimationStart(animation)
                tvNum.visibility = View.VISIBLE
            }

            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                tvNum.visibility = View.INVISIBLE
            }
        })
    }


    fun start(){
        redPacketView.startAnimator()
        redPacketView.setCallBack(object : RedPacketView.RedPacketCallBack{
            override fun start() {

            }

            override fun pause() {

            }

            override fun resume() {

            }

            override fun completed() {
                //启动文字动画
                animatorSet.start()
                //接口回调出去
                stateCallBack?.completed()
            }
        })
    }

    fun resume(){
        redPacketView.resumeAnimator()
    }

    fun pause(){
        redPacketView.pauseAnimator()
    }

    fun stop(){
        redPacketView.stopAnimator()
    }

    fun setNum(num:Int){
        tvNum.text = "+"+num
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        EventBus.getDefault().register(this) //注册
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        EventBus.getDefault().unregister(this)//解除注册
    }
    private val handler:Handler = Handler(Looper.getMainLooper())
    //订阅接受事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(event:RedPacketEvent){
        when(event.state){
            RedPacketEnum.INIT -> start()
            RedPacketEnum.PAUSE -> {
                //缓冲效果
                handler.postDelayed({
                    pause()
                },1000)
            }
            RedPacketEnum.RESUME -> resume()
            RedPacketEnum.STOP -> stop()
        }
    }

    //接口回调
    interface StateCallBack{
        fun completed()
    }
    private var stateCallBack:StateCallBack? = null
    fun setStateCallBack(stateCallBack: StateCallBack){
        this.stateCallBack = stateCallBack
    }


}